package com.huaxindata.im.main.chat.Helper;

import com.huaxindata.im.main.chat.ConversationManager;
import com.huaxindata.im.main.chat.adapter.ChatAdapter;
import com.huaxindata.im.main.manager.UserManager;
import com.huaxindata.im.model.contact.Contact;
import com.huaxindata.im.model.db.MessageStore;
import com.huaxindata.im.model.entity.ChatMessage;
import com.huaxindata.im.model.entity.ChatMessageBody;
import com.huaxindata.im.model.entity.ChatTextMessageBody;

/**
 * Created by Administrator on 2017/9/1.
 * 发送文本消息的辅助类，主要是连接ConversationActivity和
 * ChatAdapter，作为一个中间层
 */

public class TextHelper {
    /**
     * 发送文本消息
     */
    public static void sendText(String content, final ChatAdapter adapter, Contact contact) {
        if (content.length()==0) {
            return;
        }
        String sender=UserManager.getClientId();
        String receiver=contact.getClientId();
        final ChatMessage message = new ChatMessage(ChatMessageBody.Type.TEXT,sender,receiver, System.currentTimeMillis());
        ChatTextMessageBody chatTextMessageBody=new ChatTextMessageBody(ChatMessageBody.Type.TEXT);
        chatTextMessageBody.content=content;
        message.messageBody=chatTextMessageBody;
        message.read=1;
        adapter.addMessage(message);
        adapter.refreshToLast();
        MessageStore.insert(message);//将消息存入数据库
        ConversationManager.sendMessage(message);
    }
}
